%BUILDINGMULTILAYERCIRCULAR_GAP 带间隙的多层圆盘建模
%@since: 2022-04-24 19:31:21
%@update:  2022-05-03 10:20:50 忘记除以2
%@type:function
%    *参数说明*
%       @param []
%       @return []
%    *使用说明*
%       [] = BUILDINGMULTILAYERCIRCULAR_GAP
%
% ———————————————— POWERED BY YINWEI ————————————————
clear;clc;clf; close all;
mws = CST_Initilization;
tic;
CST_Modeling(mws);
disp(["建模成功，所用时间为：", num2str(toc), 'S']);

%% 建模函数
function [] = CST_Modeling(mws)

    Frq = [0.3, 3]; % 工作频率,太赫兹波波长范围是3mm-30um
    FC_Property_Setup_Simplfied(mws, Frq, 'time');
    % 仿真参数设置
    P = 300; %结构周期
    t_metal = 0.2; %基地层金属厚度
    Eachlayer = 25;
    DeltaDiameter = -30;
    MaxDiameter = 200;
    TotalHeight = 100;
    BaseHeight = 0;
    Gap = 0;
    ChannelDiameter = 45;

    % 在CST中加入结构参数，方便后续查看
    invoke(mws, 'StoreParameter', 'P', P);
    invoke(mws, 'StoreParameter', 't_metal', t_metal);
    invoke(mws, 'StoreParameter', 'Eachlayer', Eachlayer);
    invoke(mws, 'StoreParameterWithDescription', 'TotalHeight ', TotalHeight, ' 总高度');
    invoke(mws, 'StoreParameterWithDescription', 'BaseHeight ', BaseHeight, '最小层高度 ');
    invoke(mws, 'StoreParameter', 'DeltaDiameter ', DeltaDiameter);
    invoke(mws, 'StoreParameter', 'MaxDiameter ', MaxDiameter);
    invoke(mws, 'StoreParameter', 'Gap ', Gap);
    invoke(mws, 'StoreParameter', 'ChannelDiameter ', ChannelDiameter);

    % 组件
    % FC_New_Component(mws, "Ground");
    FC_New_Component(mws, "介质");
    FC_New_Component(mws, "Metal");

    %% 建立模型
    % 建立方块的函数

    FC_Model_Brick(mws, '-P/2', 'P/2', '-P/2', 'P/2', '0', 'TotalHeight', 'BIO_YW', '介质', '大树脂方块');

    for loop = 1:2
        % 建立流道入口
        FC_Model_Cylinder(mws, 'ChannelDiameter/2', '0', '0', '0', 'BaseHeight', 'TotalHeight', 'Cu_YW', 'Metal', ['流道入口 loop_' num2str(loop)]);

        % 建立多层结构
        for eachlayer = 1:3
            FC_Model_Cylinder(mws, ['MaxDiameter/2+' num2str(eachlayer - 1) '*DeltaDiameter/2'], '0', '0', '0', ['BaseHeight+' num2str(eachlayer - 1) '*(Eachlayer+Gap)'], ['BaseHeight+' num2str(eachlayer - 1) '*(Eachlayer+Gap)+Eachlayer'], 'Cu_YW', 'Metal', ['第' num2str(eachlayer) '层 loop_' num2str(loop)]);

            if loop == 2
                invoke(mws, 'AddToHistory', ['减去loop2的第' num2str(eachlayer) '层'], ['Solid.Subtract "介质:大树脂方块", "Metal:第' num2str(eachlayer) '层 loop_2"']);

            end

        end

    end

    invoke(mws, 'AddToHistory', '减去流道', 'Solid.Subtract "介质:大树脂方块", "Metal:流道入口 loop_2" ');
    SetAPort(mws);

end

%% 保存文件
function [] = CST_Save_File(mws, Path, Filename)
    fullname = [Path Filename];
    invoke(mws, 'SaveAs', fullname, 'True'); %True表示保存到目前为止的结果
end

function [] = SetAPort(mws)
    CMDs = '';
    CMDs = [CMDs 'With Port'];
    CMDs = [CMDs 10 '.Reset'];
    CMDs = [CMDs 10 '.PortNumber "1"'];
    CMDs = [CMDs 10 '.Label ""'];
    CMDs = [CMDs 10 '.Folder ""'];
    CMDs = [CMDs 10 '.NumberOfModes "1"'];
    CMDs = [CMDs 10 '.AdjustPolarization "True" '];
    CMDs = [CMDs 10 '.PolarizationAngle "90.0"'];
    CMDs = [CMDs 10 '.ReferencePlaneDistance "0"'];
    CMDs = [CMDs 10 '.TextSize "50"'];
    CMDs = [CMDs 10 '.TextMaxLimit "1"'];
    CMDs = [CMDs 10 '.Coordinates "Full"'];
    CMDs = [CMDs 10 '.Orientation "zmax"'];
    CMDs = [CMDs 10 '.PortOnBound "True"'];
    CMDs = [CMDs 10 '.ClipPickedPortToBound "False"'];
    CMDs = [CMDs 10 '.Xrange "-350", "350"'];
    CMDs = [CMDs 10 '.Yrange "-350", "350"'];
    CMDs = [CMDs 10 '.Zrange "179.9584916", "179.9584916"'];
    CMDs = [CMDs 10 '.XrangeAdd "0.0", "0.0"'];
    CMDs = [CMDs 10 '.YrangeAdd "0.0", "0.0"'];
    CMDs = [CMDs 10 '.ZrangeAdd "0.0", "0.0"'];
    CMDs = [CMDs 10 '.SingleEnded "False"'];
    CMDs = [CMDs 10 '.WaveguideMonitor "False"'];
    CMDs = [CMDs 10 '.Create'];
    CMDs = [CMDs 10 'End With'];
    invoke(mws, 'AddToHistory', ['定义PORT_1'], CMDs);
end

%% 调用CSTStudio并初始化
function mws = CST_Initilization()
    % path = pwd; % 获取当前m文件夹路径
    cst = actxserver('CSTStudio.application');
    mws = invoke(cst, 'NewMWS');
    % app = invoke(mws, 'GetApplicationName');
    % ver = invoke(mws, 'GetApplicationVersion');
    invoke(mws, 'FileNew');
    invoke(mws, 'DeleteResults'); % 删除之前的结果，防止程序停止运行

end
